package com.tasly.attendance.view.impl;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.tasly.attendance.entity.AttendanceRecordEntity;
import com.tasly.attendance.entity.DayHoursEntity;
import com.tasly.attendance.util.AttendanceUtils;
import com.tasly.attendance.view.AttendanceResultView;

public class AttendanceResultTxtFileViewImpl implements AttendanceResultView {

    private Map<String, String> id2personMap;
    
    private AttendanceResultTxtFileViewImpl() {
        this.id2personMap = AttendanceUtils.getId2PersoMap();
    }
    
    public static AttendanceResultView getInstance() {
        return new AttendanceResultTxtFileViewImpl();
    }
    
    @Override
    public void viewOriginalData(Map<String, List<AttendanceRecordEntity>> originalMap, Object object) {
        if (object == null && !(object instanceof File)) {
            System.out.println("Exception occurred during output original data!!");
        } else {
            File outputFile = (File)object;
            BufferedWriter bufferedWriter = null;
            Set<Entry<String, List<AttendanceRecordEntity>>> attendanceRecordEntitysetSet = originalMap.entrySet();
            
            try {
                bufferedWriter  = new BufferedWriter(new FileWriter(outputFile));
                for (Entry<String, List<AttendanceRecordEntity>> entry : attendanceRecordEntitysetSet) {
                    bufferedWriter.write("Person Name: " + id2personMap.get(entry.getKey()) +"\n");
                    for (AttendanceRecordEntity record : entry.getValue()) {
                        bufferedWriter.write("working datetime: "+record.getWorkingTime()+"\n");
                    }
                    bufferedWriter.write("\n\n");
                }
            } catch (IOException e) {
                System.out.println("CANNOT write orignal data to txt file!!");
            } finally {
                if (bufferedWriter != null) {
                    try {
                        bufferedWriter.close();                        
                    } catch (Exception e2) {
                        System.out.println("Exception happened when try to close original data txt writer!!");
                   }
                }
            }
        }
    }

    @Override
    public void viewAnalysedData(Map<String, List<DayHoursEntity>> analysedMap, Object object) {
        if (object == null && !(object instanceof File)) {
            System.out.println("Exception occurred during output original data!!");
        } else {
            File outputFile = (File)object;
            BufferedWriter bufferedWriter = null;
            Set<Entry<String, List<DayHoursEntity>>> dayHoursEntitysetSet = analysedMap.entrySet();
            
            try {
                bufferedWriter  = new BufferedWriter(new FileWriter(outputFile));
                for (Entry<String, List<DayHoursEntity>> entry : dayHoursEntitysetSet) {
                    bufferedWriter.write("Person Name: "+ id2personMap.get(entry.getKey()) +"\n");
                    for (DayHoursEntity record : entry.getValue()) {
                        bufferedWriter.write("Day Of Month: "+record.getDayOfMonth() + " | Working hours: "+record.getHours() +"\n");
                    }
                    bufferedWriter.write("\n\n");
                }
            } catch (IOException e) {
                System.out.println("CANNOT write orignal data to txt file!!");
            }finally {
                if (bufferedWriter != null) {
                    try {
                        bufferedWriter.close();                        
                    } catch (Exception e2) {
                        System.out.println("Exception happened when try to close analysed data txt writer!!");
                   }
                }
            }
        }
    }

}
